<!DOCTYPE html>
<html lang="en">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<!-- Meta, title, CSS, favicons, etc. -->
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">

		<title>添加合作公司页面 </title>

		<!-- Bootstrap -->
		<link href="css/bootstrap.min.css" rel="stylesheet">
		<!-- Font Awesome -->
		<link href="css/font-awesome.min.css" rel="stylesheet">
		<!-- NProgress -->
		<!--<link href="css/nprogress.css" rel="stylesheet">-->

		<!-- Custom Theme Style -->
		<link href="css/custom.min.css" rel="stylesheet">
	</head>

	<body class="nav-md">
		<div class="container body">
			<div class="main_container">
				<div id="head">
				</div>

				<!-- top navigation -->

				<!-- /top navigation -->
				<!-- page content -->
				<div class="right_col" role="main">
					<div class="">
						<div class="page-title">
							<div class="title_left">
								<h3 style="color: green;">添加合作公司</h3>
							</div>

							<div class="title_right">
								<div class="col-md-5 col-sm-5 col-xs-12 form-group pull-right top_search">

								</div>
							</div>
						</div>
						<div class="clearfix"></div>

						<div class="row">
							<div class="col-md-12 col-sm-12 col-xs-12">
								<div class="x_panel">
									<div class="x_title">

									</div>
									<div class="x_content">

										<form class="form-horizontal form-label-left" novalidate>


											<div class="item form-group">
												<label class="control-label col-md-3 col-sm-3 col-xs-12" >公司名 <span class="required">*</span>
												</label>
												<div class="col-md-6 col-sm-6 col-xs-12">
													<input type="text" style="width: 210px;" placeholder="请填写公司名" id="cName" class="form-control col-md-7 col-xs-12">
													<span id="cNameError" style="margin-left: 20px;line-height: 34px;color: red;display: none"></span>
												</div>
											</div>
											<div class="item form-group">
												<label class="control-label col-md-3 col-sm-3 col-xs-12" >法人姓名 <span class="required">*</span>
												</label>
												<div class="col-md-6 col-sm-6 col-xs-12">
													<input type="text" style="width: 210px;" placeholder="请填写法人姓名" id="pName" class="form-control col-md-7 col-xs-12">
													<span id="pNameError" style="margin-left: 20px;line-height: 34px;color: red;display: none"></span>
												</div>
											</div>
											<div class="item form-group">
												<label class="control-label col-md-3 col-sm-3 col-xs-12" >法人联系方式 <span class="required">*</span>
												</label>
												<div class="col-md-6 col-sm-6 col-xs-12">
													<input type="tel" style="width: 210px;" id="pPhone" placeholder="请填写法人联系方式" class="form-control col-md-7 col-xs-12">
													<span id="pPhoneError" style="margin-left: 20px;line-height: 34px;color: red;display: none"></span>
												</div>
											</div>
											<div class="item form-group">
												<label class="control-label col-md-3 col-sm-3 col-xs-12" >公司所在区域 <span class="required">*</span>
												</label>
												<div class="col-md-6 col-sm-6 col-xs-12" id="cAddress">
													<select id="s_province" name="s_province" style="padding: 6px 2px">
													</select>
													<select id="s_city" name="s_city" style="padding: 6px 2px">
													</select>
													<select id="s_county" name="s_county" style="padding: 6px 2px">
													</select>
													<span id="cAddressError" style="margin-left: 20px;line-height: 34px;color: red;display: none"></span>
												</div>
											</div>
											
											
											<div class="item form-group">
												<label class="control-label col-md-3 col-sm-3 col-xs-12" >公司类型 <span class="required">*</span>
												</label>
												<div class="col-md-6 col-sm-6 col-xs-12">
													<select style="height: 30px;" id="cType">
														<option value="0">请选择</option>
														<option value="1">船公司</option>
														<option value="2">航空公司</option>
													</select>
													<span id="cTypeError" style="margin-left: 20px;line-height: 34px;color: red;display: none"></span>
												</div>

											</div>

											<span id="addCompanyError" style="color: red;margin-left: 400px;font-size: 20px;"></span>
											<div class="ln_solid"></div>
											<div class="form-group">
												<div class="col-md-6 col-md-offset-3">
													<a id="addCompany" type="button" class="btn btn-success">新增</a>
													<button type="button" class="btn btn-primary" onclick="window.location.href='showcompany.html'">返回</button>
												</div>
											</div>
										</form>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
				<!-- /page content -->

				<!-- footer content -->
				<footer>
					<div class="pull-right">
						Gentelella - Bootstrap Admin Template by <a href="https://colorlib.com">Colorlib</a>
					</div>
					<div class="clearfix"></div>
				</footer>
				<!-- /footer content -->
			</div>
		</div>

		<!-- jQuery -->
		<script src="js/jquery.min.js"></script>
		<!-- Bootstrap -->
		<script src="js/bootstrap.min.js"></script>
		<!-- FastClick -->
		<script src="js/fastclick.js"></script>
		<!-- NProgress -->
		<script src="js/nprogress.js"></script>
		<!-- validator -->

		<script src="js/area.ui.js"></script>
		<!-- Custom Theme Scripts -->

		<!-- validator -->
		<script>

            $(function(){
                $(document).area("s_province","s_city","s_county");//调用插件
                $('.cancel').click(function(){
                    var str="";
                    $('#s_province').children().each(function(){
                        if($(this).prop('selected')==true){
                            str+=$(this).html();
                        }
                    });
                    $('#s_city').children().each(function(){
                        if($(this).prop('selected')==true){
                            str+=$(this).html();
                        }
                    });
                    $('#s_county').children().each(function(){
                        if($(this).prop('selected')==true){
                            str+=$(this).html();
                        }
                    });
                    str += document.getElementById('all').value;
                    $('#time').val(str);

                });
            });
            //地区三级联动结束

            var $token=window.sessionStorage.getItem('token');

			$("#cName").blur(function() {
				var $cName = $("#cName").val();
				if ($cName == null || $cName == ""){
					$("#cNameError").show();
                    $("#cNameError").html("请填写公司名字");
				}else {
                    $("#cNameError").hide();
				}
            });

            $("#pName").blur(function() {
                var $pName = $("#pName").val();
                if ($pName == null || $pName == ""){
                    $("#pNameError").show();
                    $("#pNameError").html("请填写法人姓名");
                }else {
                    $("#pNameError").hide();
                }
            });

            $("#pPhone").blur(function() {
                var $pPhone = $("#pPhone").val();
                if ($pPhone == null || !(/^1[34578]\d{9}$/.test($pPhone))){
                    $("#pPhoneError").show();
                    $("#pPhoneError").html("请填写正确的联系方式");
                }else {
                    $("#pPhoneError").hide();
                }
            });

            $("#cType").blur(function() {
                var $cType = $("#cType").val();
                if ($cType == 0){
                    $("#cTypeError").show();
                    $("#cTypeError").html("请选择公司类型");
                }else {
                    $("#cTypeError").hide();
                }
            });

			//添加公司AJAX
			$("#addCompany").click(function() {
                var $cName = $("#cName").val();
                var $pName = $("#pName").val();
                var $pPhone = $("#pPhone").val();
                var $cType = $("#cType").val();
                var $one = $("#s_province").select().val();
                var $two = $("#s_city").select().val();
                var $three = $("#s_county").select().val();
				var $cAddress = $one+$two+$three;
				if ($cName==""){
                    $("#cNameError").show();
                    $("#cNameError").html("请填写公司名字");
                    return;
				}else if ($pName==""){
                    $("#pNameError").show();
                    $("#pNameError").html("请填写法人姓名");
                    return;
				}else if ($pPhone==""){
                    $("#pPhoneError").show();
                    $("#pPhoneError").html("请填写正确的联系方式");
                    return;
                }else if ($cType==0){
                    $("#cTypeError").show();
                    $("#cTypeError").html("请选择公司类型");
                    return;
                }else if ($two == null){
                    $("#cAddressError").show();
                    $("#cAddressError").html("请选择正确的地址");
                    return;
				}else {
                    $("#cAddressError").hide();
                    $.ajax({
                        type:"post",
                        xhrFields:{withCredentials:true},
                        url:"http://localhost:8081/company/addCompany",
                        dataType:"json",
                        data:{"cName":$cName,"cType":$cType,"pName":$pName,"pPhone":$pPhone,"cAddress":$cAddress,"token":$token},
                        success:function(data) {
                            if(data.message=="no"){
                                $("#addCompanyError").show();
                                $("#addCompanyError").html("请不要重复添加");
                                return;
                            }else if(data.message=="yes"){
                                window.location.href='showcompany.html';
                            }else{
                                $("#addCompanyError").show();
                                $("#addCompanyError").html("添加失败");
							}
                        },
                        error:function(){
                            alert("异常");

                        }
                    });
				}
            });

			$(document).ready(function() {
				$("#head").load("head.html");
			});
			// initialize the validator function
			// validator.message.date = 'not a real date';

			// validate a field on "blur" event, a 'select' on 'change' event & a '.reuired' classed multifield on 'keyup':
			// $('form')
			// 	.on('blur', 'input[required], input.optional, select.required', validator.checkField)
			// 	.on('change', 'select.required', validator.checkField)
			// 	.on('keypress', 'input[required][pattern]', validator.keypress);

			$('.multi.required').on('keyup blur', 'input', function() {
				validator.checkField.apply($(this).siblings().last()[0]);
			});

			$('form').submit(function(e) {
				e.preventDefault();
				var submit = true;

				// evaluate the form using generic validaing
				if (!validator.checkAll($(this))) {
					submit = false;
				}

				if (submit)
					this.submit();

				return false;
			});
		</script>
		<!-- /validator -->
	</body>
</html>
